/**
 * Created by Aimee on 14-11-14.
 */

require(['jquery', 'jquerymobile', 'net', 'dialogs'], function($, m, net, dia) {


    //点击菜单 推出push页面
    $('#activity_filter_btn_menu').on('click', function(evt) {
        $('.ui-panel-inner').css({'padding':'0px'});
        $('.push_nees_opacity').css('min-height',$(window).height());
        $('#setting_content').css('max-height',$(window).height()-40);
        $('#activity_filter_panel_menu').css('min-height',$(window).height());
        $('#filter_activity_push_ul').css('min-height',$(window).height());
        $('#filter_activity_auto_height').css('min-height',$(window).height()-354);
        console.log($('#activity_filter_panel_menu').css('min-height'));
        console.log($('#filter_activity_push_ul').css('min-height'));
        console.log($('#filter_activity_auto_height').css('height'));
        var username = localStorage['username'] + '!';
        $("#filter_activity_push_username").html(username);
        $( "#activity_filter_panel_menu" ).panel( "open" );

    });

    // 点击新闻项，菜单隐藏，推出新闻页
    $('#filter_activity_push_newsroom').on('click', function(evt) {
        $( "#activity_filter_panel_menu" ).panel( "close" );
        // Nick added for pull to refresh start
        initPageLoading("news_wrapper");
        // Nick added for pull to refresh end
        $.mobile.newChangePage("#newsroom",{ transition: "slide",reverse: true,changeHash: false});
    });

    // 点击活动项，菜单隐藏，推出活动页
    $('#filter_activity_push_activities').on('click', function(evt) {
        $( "#activity_filter_panel_menu" ).panel( "close" );
        // Nick added for pull to refresh start
        initPageLoading("activities_wrapper");
        // Nick added for pull to refresh end
        $.mobile.newChangePage("#activities",{ transition: "slide",reverse: true,changeHash: false});
    });
	$('#filter_activity_push_AR').on('click', function(evt) {
        cordova.exec(function(){cordova.logger.debug("success");}, function(){cordova.logger.debug("error");}, "ShowVideoPlaybackView", "showVideoPlaybackView", []);
    });

    $('#filter_activity_push_Laisee').on('click', function(evt) {
        //Green Laisee entrance
        var cuser = null;
        if (localStorage.getItem('login_user')!=null) {
            cuser = JSON.parse(localStorage.getItem('login_user'));
        } else {
            console.assert(false, '还没登录，没有用户信息可填写');
        }
        cordova.exec(function(){console.log("success")}, function(){console.log("error")}, "OpenGreenLaisee", "openGreenLaisee", [JSON.stringify({"account": ""+cuser.staffId+"", "fullName": ""+cuser.english_name+"", "secureToken": "", "location": "CN"})]);
    });

    // 点击注销，返回登录
    $('#filter_activity_push_logon').on('click', function(evt) {
        $.mobile.newChangePage("#logon",{ transition: "slide",reverse: true,changeHash: false});
        clear_checkbox();
        // Nick added for pull to refresh start
        cleanUpListItems();
        $(".wrapper").find("#pullUp").hide();
        window.shouldPageRefresh.newsroom = true;
        window.shouldPageRefresh.activities = true;
        // Nick added for pull to refresh end
    });

    //注销用户后复选框清空
    function clear_checkbox() {
        $('#activity_all_check').removeClass('nor').addClass('sel');
        $('div[name="sbox"]', $('#activity_filter')).each(function(index, val) {
            $(val).removeClass('nor').addClass('sel');
        });
        //$('#activity_filter_btn_applay').hide();
    }

    //推出setting页面
    $('#filter_activity_push_setting').on('click', function(evt) {
        $.mobile.newChangePage("#setting",{ transition: "slide",reverse: false,changeHash: false});
    });

    //changePassword页面 设置内容高度是Header剩下的高度
    $("#activity_filter").on( "pageshow", function( event ) {
        window.setBodyOverflow($(document.body));
        $('#activity_filter_content').css('height',($(window).height()-40));
        $('#activityFilter_ul').css('min-height', ($(window).height()-41));

        // 读取以前勾的数据
        var selectBefore = [];
        if (!$.isEmptyObject(localStorage['activity_selected_tags'])) {
            selectBefore = localStorage['activity_selected_tags'].toString().split(',');

            $.each($('div[name="sbox"]', $('#activity_filter')), function(boxIndex, boxData) {
                $.each(selectBefore, function(selIndex, selectData) {
                    if (selectData == $(boxData).attr('alt')) {
                        $(boxData).removeClass('nor').addClass('sel');
                    }
                });
            });

            if ($('div[name="sbox"]', $('#activity_filter')).length == (selectBefore.length - 1)) {
                $('div[name="sbox2"]', $('#activity_filter')).removeClass('nor').addClass('sel');
            } else {
                $('div[name="sbox2"]', $('#activity_filter')).removeClass('sel').addClass('nor');
            }
        } else {
            clear_checkbox();
        }

        $('div[name="sbox"]', $('#activity_filter')).off('click');
        $('div[name="sbox"]', $('#activity_filter')).on('click', function(event) {
            if ($(this).hasClass('nor'))
                $(this).removeClass('nor').addClass('sel');
            else
                $(this).removeClass('sel').addClass('nor');

            // 如果勾选了数量等于总数 则全选择也勾上
            var selectCount = 0;
            $.each($('div[name="sbox"]', $('#activity_filter')), function(boxIndex, boxData) {
                if ($(boxData).hasClass('sel')) {
                    selectCount++;
                }
            });
            if (selectCount === 0) {
                $('#activity_filter_btn_applay').hide();
            } else {
                $('#activity_filter_btn_applay').show();
            }
            if ($('div[name="sbox"]', $('#activity_filter')).length == selectCount) {
                $('div[name="sbox2"]', $('#activity_filter')).removeClass('nor').addClass('sel');
            } else {
                $('div[name="sbox2"]', $('#activity_filter')).removeClass('sel').addClass('nor');
            }
        });

        $('#activity_all_check').off('click');
        $('#activity_all_check').on('click', function(event) {
            console.log('是走了');
            if ($('#activity_all_check').hasClass('nor')) {
                console.log('本来就没选择');
                $(this).removeClass('nor').addClass('sel');
                $('div[name="sbox"]', $('#activity_filter')).each(function(index, val) {
                    $(val).removeClass('nor').addClass('sel');
                });
                $('#activity_filter_btn_applay').show();
            } else {
                console.log('本来选择了');
                $(this).removeClass('sel').addClass('nor');
                $('div[name="sbox"]', $('#activity_filter')).each(function(index, val) {
                    $(val).removeClass('sel').addClass('nor');
                });
                $('#activity_filter_btn_applay').hide();
            }


        });
        // Nick added for pull to refresh start
        window.shouldPageRefresh.newsroom = true;
        window.shouldPageRefresh.activities = true;
        // Nick added for pull to refresh end
    });

    // Nick added for pull to refresh start
    function cleanUpListItems() {
        $("#newsroom_listview_news").empty();
        $("#activities_listview_news").empty();
    }

    function initPageLoading(wrapper) {
        var $wrapper = $("#" + wrapper),
            $pullDownEl = $wrapper.find("#pullDown"),
            $pullDownLabel = $pullDownEl.find(".pullDownLabel");

        if ($wrapper.find("ul").html()) {
            $wrapper.find(".scroller").css(window.getVendorStyle("transform"), "translate(0, 0)");
            $pullDownEl.attr("class", "loading");
            $pullDownLabel.text("Loading...")
        }
    }
    // Nick added for pull to refresh end

    //applay 按钮返回新闻
    $('#activity_filter_btn_applay').on('click', function(evt) {
        var sel_tags = '';
        $.each($('div[name="sbox"]', $('#activity_filter')), function(index, val) {

            if ($(this).hasClass('sel')) {
                sel_tags += $(this).attr('alt')+',';
            }
        });
        localStorage['activity_selected_tags'] = sel_tags;
        // Nick added for pull to refresh start
        initPageLoading("activities_wrapper");
        // Nick added for pull to refresh end
        $.mobile.newChangePage("#activities",{ transition: "slide",reverse: true,changeHash: false});
    });



    function compatibility() {
        /* Logon */
        $('#activity_title_filter').parent()
            .css('display', 'block')
            .css('postion', 'relative');

        $('#activity_title_filter').css('postion', 'absulute')
            .css('width', '198px')
            .css('height','20px')
            .css('margin', '8px auto auto auto');
    }

    $(document).ready(function () {
        setTimeout(function() {
            // 兼容其他浏览器
            compatibility();
        },1000);
    });

});

